#include <iostream>
#include <vector>
#include <string>

using namespace std;

namespace BinarySearchTree
{
	template<class T>
	struct BSTreeNode
	{
		T _key;
		BSTreeNode<T>* _left;
		BSTreeNode<T>* _right;

		BSTreeNode(const T& key)
			:_key(key)
			,_left(nullptr)
			,_right(nullptr)
		{}
	};

	template<class T>
	class keyBSTree
	{
		typedef BSTreeNode<T> Node;
	public:

		bool find(const T& key)
		{
			Node* cur = _root;
			while (cur)
			{
				if (cur->_key < key)
				{
					cur = cur->_right;
				}
				else if (cur->_key > key)
				{
					cur = cur->_left;
				}
				else
				{
					return true;
				}
			}

			return false;
		}

		bool insert(const T& key)
		{
			if (_root == nullptr)
			{
				_root = new Node(key);
				return true;
			}

			Node* cur = _root;
			Node* parent = nullptr;

			while (cur)
			{
				if (cur->_key < key)
				{
					parent = cur;
					cur = cur->_right;
				}
				else if (cur->_key > key)
				{
					parent = cur;
					cur = cur->_left;
				}
				else
				{
					return false;
				}
			}

			cur = new Node(key);
			if (parent->_key < key)
			{
				parent->_right = cur;
			}
			else
			{
				parent->_left = cur;
			}

			return true;
		}

		void InOrder()
		{
			_InOrder(_root);
			cout << endl;
		}

		bool erase(const T& key)
		{
			Node* cur = _root;
			Node* parent = nullptr;
			while (cur)
			{
				if (cur->_key < key)
				{
					parent = cur;
					cur = cur->_right;
				}
				else if (cur->_key > key)
				{
					parent = cur;
					cur = cur->_left;
				}
				else
				{
					if (cur->_left == nullptr)
					{
						if (cur == _root)
						{
							_root = cur->_right;
						}
						else
						{
							if (parent->_left == cur)
							{
								parent->_left = cur->_right;
							}
							else
							{
								parent->_right = cur->_right;
							}
						}

						delete cur;
					}
					else if (cur->_right == nullptr)
					{
						if (cur == _root)
						{
							_root = cur->_left;
						}
						else
						{
							if (parent->_left == cur)
							{
								parent->_left = cur->_left;
							}
							else
							{
								parent->_right = cur->_left;
							}
						}

						delete cur;
					}
					else
					{
						Node* min = cur->_right;
						Node* minparent = cur;

						while (min->_left)
						{
							minparent = min;
							min = min->_left;
						}

						swap(cur->_key, min->_key);
						if (minparent->_right == min)
						{
							minparent->_right = min->_right;
						}
						else
						{
							minparent->_left = min->_right;
						}

						delete min;
					}

					return true;
				}
			}

			return false;
		}

		bool findR(const T& key)
		{
			return _findR(_root, key);
		}

		bool insertR(const T& key)
		{
			return _insertR(_root, key);
		}

		bool eraseR(const T& key)
		{
			return _eraseR(_root, key);
		}

		~keyBSTree()
		{
			destoryR(_root);
		}

		// 只要写了构造，编译器就不会生成默认构造
		// 强制编译器生成默认构造
		keyBSTree() = default;

		keyBSTree(const keyBSTree<T>& tree)
		{
			_root = _copy(tree._root);
		}

		keyBSTree& operator=(keyBSTree tree)
		{
			swap(_root, tree._root);
			return *this;
		}

	private:

		Node* _copy(Node* root)
		{
			if (root == nullptr)
			{
				return nullptr;
			}

			Node* node = new Node(root->_key);
			node->_left = _copy(root->_left);
			node->_right = _copy(root->_right);
			return node;
		}

		bool _eraseR(Node*& root, const T& key)
		{
			if (root == nullptr)
			{
				return false;
			}

			if (root->_key < key)
				return _eraseR(root->_right, key);
			else if (root->_key > key)
				return _eraseR(root->_left, key);
			else
			{
				Node* del = root;
				if (root->_left == nullptr)
				{
					root = root->_right;
				}
				else if (root->_right == nullptr)
				{
					root = root->_left;
				}
				else
				{
					// 右子树找最小
					Node* min = root->_right;

					while (min->_left)
					{
						min = min->_left;
					}

					swap(root->_key, min->_key);
					return _eraseR(root->_right, key);
				}

				delete del;
				return true;
			}
		}

		void destoryR(Node*& root)
		{
			if (root == nullptr)
				return;

			destoryR(root->_left);
			destoryR(root->_right);
			delete root;
			root = nullptr;
		}

		bool _insertR(Node*& root, const T& key)
		{
			if (root == nullptr)
			{
				root = new Node(key);
				return true;
			}

			if (root->_key > key)
			{
				return _insertR(root->_left, key);
			}
			else if (root->_key < key)
			{
				return _insertR(root->_right, key);
			}
			else
			{
				return false;
			}
		}

		bool _findR(Node* root, const T& key)
		{
			if (root == nullptr)
				return false;

			if (root->_key > key)
			{
		    	return _findR(root->_left, key);
			}
			else if (root->_key < key)
			{
				return _findR(root->_right, key);
			}
			else
			{
				return true;
			}
		}

		void _InOrder(Node* root)
		{
			if (root == nullptr)
			{
				return;
			}

			_InOrder(root->_left);
			cout << root->_key << ' ';
			_InOrder(root->_right);
		}

		Node* _root = nullptr;
	};

	template<class K, class V>
	struct DBSTreeNode
	{
		K _key;
		V _value;

		DBSTreeNode<K, V>* _left;
		DBSTreeNode<K, V>* _right;

		DBSTreeNode(const K& key, const V& value)
			:_key(key)
			,_value(value)
			,_left(nullptr)
			,_right(nullptr)
		{}
	};

	template<class T, class V>
	class kVBSTree
	{
		typedef DBSTreeNode<T, V> Node;
	public:

		Node* find(const T& key)
		{
			Node* cur = _root;
			while (cur)
			{
				if (cur->_key < key)
				{
					cur = cur->_right;
				}
				else if (cur->_key > key)
				{
					cur = cur->_left;
				}
				else
				{
					return cur;
				}
			}

			return nullptr;
		}

		bool insert(const T& key, const V& value)
		{
			if (_root == nullptr)
			{
				_root = new Node(key, value);
				return true;
			}

			Node* cur = _root;
			Node* parent = nullptr;

			while (cur)
			{
				if (cur->_key < key)
				{
					parent = cur;
					cur = cur->_right;
				}
				else if (cur->_key > key)
				{
					parent = cur;
					cur = cur->_left;
				}
				else
				{
					return false;
				}
			}

			cur = new Node(key, value);
			if (parent->_key < key)
			{
				parent->_right = cur;
			}
			else
			{
				parent->_left = cur;
			}

			return true;
		}

		void InOrder()
		{
			_InOrder(_root);
			cout << endl;
		}

		bool erase(const T& key)
		{
			Node* cur = _root;
			Node* parent = nullptr;
			while (cur)
			{
				if (cur->_key < key)
				{
					parent = cur;
					cur = cur->_right;
				}
				else if (cur->_key > key)
				{
					parent = cur;
					cur = cur->_left;
				}
				else
				{
					if (cur->_left == nullptr)
					{
						if (cur == _root)
						{
							_root = cur->_right;
						}
						else
						{
							if (parent->_left == cur)
							{
								parent->_left = cur->_right;
							}
							else
							{
								parent->_right = cur->_right;
							}
						}

						delete cur;
					}
					else if (cur->_right == nullptr)
					{
						if (cur == _root)
						{
							_root = cur->_left;
						}
						else
						{
							if (parent->_left == cur)
							{
								parent->_left = cur->_left;
							}
							else
							{
								parent->_right = cur->_left;
							}
						}

						delete cur;
					}
					else
					{
						Node* min = cur->_right;
						Node* minparent = cur;

						while (min->_left)
						{
							minparent = min;
							min = min->_left;
						}

						swap(cur->_key, min->_key);
						if (minparent->_right == min)
						{
							minparent->_right = min->_right;
						}
						else
						{
							minparent->_left = min->_right;
						}

						delete min;
					}

					return true;
				}
			}

			return false;
		}

		~kVBSTree()
		{
			destoryR(_root);
		}

		// 只要写了构造，编译器就不会生成默认构造
		// 强制编译器生成默认构造
		kVBSTree() = default;

		kVBSTree(const kVBSTree<T, V>& tree)
		{
			_root = _copy(tree._root);
		}

		kVBSTree& operator=(kVBSTree tree)
		{
			swap(_root, tree._root);
			return *this;
		}

	private:

		Node* _copy(Node* root)
		{
			if (root == nullptr)
			{
				return nullptr;
			}

			Node* node = new Node(root->_key, root->_value);
			node->_left = _copy(root->_left);
			node->_right = _copy(root->_right);
			return node;
		}

		void destoryR(Node*& root)
		{
			if (root == nullptr)
				return;

			destoryR(root->_left);
			destoryR(root->_right);
			delete root;
			root = nullptr;
		}

		void _InOrder(Node* root)
		{
			if (root == nullptr)
			{
				return;
			}

			_InOrder(root->_left);
			cout << root->_key << ' ' << root->_value << ' ' << '|' << ' ';
			_InOrder(root->_right);
		}

		Node* _root = nullptr;
	};
}
